<?php
ini_set('date.timezone','Asia/Shanghai');
//error_reporting(E_ERROR);
require_once "../lib/WxPay.Api.php";
require_once "WxPay.JsApiPay.php";
require_once 'log.php';

//初始化日志
$logHandler= new CLogFileHandler("../logs/".date('Y-m-d').'.log');
$log = Log::Init($logHandler, 15);

//打印输出数组信息
function printf_info($data)
{
    foreach($data as $key=>$value){
        echo "<font color='#00ff55;'>$key</font> : $value <br/>";
    }
}

//①、获取用户openid
$tools = new JsApiPay();
$openId = $tools->GetOpenid();

//获取订单
include("../../include/config.inc.php");
include("../../user/userlogincheck.php");
$state = $_GET['state'];//("userid" => $userid, "orderNo" => $orderNo, "total_fee" =>"$total_fee", "body" =>"$body")
$json_string=$state; 
$obj=json_decode($json_string);
$orderNo=$obj->orderNo;

$sql_order="SELECT * FROM ".tableprefix."order where orderNo='$orderNo';";
$result_order=$db->query($sql_order);
$order=$db->fetch_array($result_order);
$amount=$order["amount"];
$total_fee=$amount*100;

//②、统一下单
$input = new WxPayUnifiedOrder();
$input->SetBody("入驻保证金");
$input->SetAttach("入驻保证金");
//$input->SetOut_trade_no(WxPayConfig::MCHID.date("YmdHis"));
$input->SetOut_trade_no($orderNo);//11708161647411000030-146572430220170816164741:146572430220170816164740
$input->SetTotal_fee($total_fee);
$input->SetTime_start(date("YmdHis"));
$input->SetTime_expire(date("YmdHis", time() + 600));
$input->SetGoods_tag("入驻保证金");
$input->SetNotify_url("http://dx.wycn.com/pay/wxpay/notify.php");
$input->SetTrade_type("JSAPI");
$input->SetOpenid($openId);
$order = WxPayApi::unifiedOrder($input);
//echo '<font color="#f00"><b>统一下单支付单信息</b></font><br/>';
//printf_info($order);
$jsApiParameters = $tools->GetJsApiParameters($order);

//获取共享收货地址js函数参数
$editAddress = $tools->GetEditAddressParameters();

//③、在支持成功回调通知中处理成功之后的事宜，见 notify.php
/**
 * 注意：
 * 1、当你的回调地址不可访问的时候，回调通知会失败，可以通过查询订单来确认支付是否成功
 * 2、jsapi支付时需要填入用户openid，WxPay.JsApiPay.php中有获取openid流程 （文档可以参考微信公众平台“网页授权接口”，
 * 参考http://mp.weixin.qq.com/wiki/17/c0f37d5704f0b64713d5d2c37b468d75.html）
 */
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>支付保证金 - 订戏会</title>
<meta name="renderer" content="webkit">
<meta http-equiv="X-UA-Compatible" content="IE=9,Chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no">
<meta name="apple-mobile-web-app-capable" content="yes" />
<meta name="apple-mobile-web-app-status-bar-style" content="black" />
<link rel="stylesheet" type="text/css" href="../../css/public.css">
<link rel="stylesheet" type="text/css" href="../../css/publish.css">
    <script type="text/javascript">
	//调用微信JS api 支付
	function jsApiCall()
	{
		WeixinJSBridge.invoke(
			'getBrandWCPayRequest',
			<?php echo $jsApiParameters; ?>,
			function(res){
				WeixinJSBridge.log(res.err_msg);
				//alert(res.err_code+res.err_desc+res.err_msg);
				location.href='http://dx.wycn.com/publish/paystatus.php?orderNo=<?php echo $orderNo;?>';
			}
		);
	}

	function callpay()
	{
		if (typeof WeixinJSBridge == "undefined"){
		    if( document.addEventListener ){
		        document.addEventListener('WeixinJSBridgeReady', jsApiCall, false);
		    }else if (document.attachEvent){
		        document.attachEvent('WeixinJSBridgeReady', jsApiCall); 
		        document.attachEvent('onWeixinJSBridgeReady', jsApiCall);
		    }
		}else{
		    jsApiCall();
		}
	}
	</script>
	<script type="text/javascript">
	//获取共享地址
	function editAddress()
	{
		WeixinJSBridge.invoke(
			'editAddress',
			<?php echo $editAddress; ?>,
			function(res){
				var value1 = res.proviceFirstStageName;
				var value2 = res.addressCitySecondStageName;
				var value3 = res.addressCountiesThirdStageName;
				var value4 = res.addressDetailInfo;
				var tel = res.telNumber;
				
				alert(value1 + value2 + value3 + value4 + ":" + tel);
			}
		);
	}
	
	window.onload = function(){
		if (typeof WeixinJSBridge == "undefined"){
		    if( document.addEventListener ){
		        document.addEventListener('WeixinJSBridgeReady', editAddress, false);
		    }else if (document.attachEvent){
		        document.attachEvent('WeixinJSBridgeReady', editAddress); 
		        document.attachEvent('onWeixinJSBridgeReady', editAddress);
		    }
		}else{
			editAddress();
		}
	};
	
	</script>
</head>
<body>
<div class="head-nav">
	<div class="head-left"><a href="#1"><div class="back">返回</div></a></div>
	<div class="head-center">商家入驻</div>
	<div class="head-right"><a href="#"><div class="shuoming"></div></a></div>
</div>
<div class="remind"><p>提交保证金，提高诚信度，获取更多用户信任。</p></div>


<div class="mod-form">
	<div class="form-group">
		<div class="form-item">
			<label>商家名称：</label>
			<input type="text" id="store_name" name="store_name" value="<?php echo GetValueByKey(tableprefix."store","member_id",$userid,"store_name");?>" readonly="readonly">
		</div>		
		<div class="form-item">
			<label>保证金：</label><font style="padding:0px 10px;color:#FF0000;"><?php echo number_format(($total_fee/100),2);?></font>元
		</div>
	</div>
	<div class="form-button"><button type="button" onClick="callpay()" style="width:95%;height:40px;line-height:40px;font-size:16px;background:#3999FD;border:0;border-radius:5px;color:#FFFFFF;font-family:'微软雅黑';">支付保证金</button></div>
	
	
	<div class="form-readme"><p>我已阅读并同意<font color="#0099FF">《网演中国商家入驻协议》</font></p></div>
</div>

<footer id="bar-fixed">
	<a href="../../mobile" class="tab-item"><i class="icon-tabbar bar-home"></i>首页</a>
	<a href="../../category.php" class="tab-item"><i class="icon-tabbar bar-message"></i>分类</a>
	<a href="../../publish" class="tab-item"><i class="icon-tabbar bar-publish"></i>发布</a>
	<a href="../../user" class="tab-item active"><i class="icon-tabbar bar-user2"></i>我</a>
</footer>
</body>
</html>